package com.shjn.squirrel.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shjn.squirrel.R;
import com.shjn.squirrel.application.MyApplication;
import com.shjn.squirrel.bean.httpBean.PhoneNumLoginReq;
import com.shjn.squirrel.bean.httpBean.RespCommonResult;
import com.shjn.squirrel.model.Constant;
import com.shjn.squirrel.model.LocalStorage;
import com.shjn.squirrel.model.UserInfoCache;
import com.shjn.squirrel.utils.OkhttpHelper;
import com.shjn.squirrel.utils.SoundPlayUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 手机号正确，提示对话框
 */
public class UserTelRightDialog extends BaseDialogFragment implements View.OnClickListener {

    private static UserTelRightDialog instance;
    private Thread mThread;
    private Boolean mIsConfirm = false;
    private TextView mTelNum;


    public UserTelRightDialog() {
    }

    public synchronized static UserTelRightDialog getInstance() {
        if (instance == null) {
            instance = new UserTelRightDialog();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //设置对话框资源文件
        setLayoutResource(R.layout.user_tel_right_dialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onResume() {
        //设置对话框大小
        setdialogSize(247, 200, -43);
        //初始化控件
        ((Button) mDialog.findViewById(R.id.btn_ok)).setOnClickListener(this);
        ((Button) mDialog.findViewById(R.id.btn_close)).setOnClickListener(this);
        mTelNum = (TextView) mDialog.findViewById(R.id.tv_tel_num);
        mTelNum.setText(UserInfoCache.telNum.substring(0, 3) + " " + UserInfoCache.telNum.substring(3, 7) +
                " " + UserInfoCache.telNum.substring(7, 11));
        //默认40秒自动关闭对话框
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(40000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mDialog.cancel();
                        mDialog.dismiss();
                        if (mIsConfirm) {
                            //确认登录，开箱
                            mIsConfirm = false;

                            //----------测试用-------------//
                            UserInfoCache.loginState = true;
                            if (UserInfoCache.category.equals(Constant.bottle)) {
                                SoundPlayUtils.play(17);//饮料瓶投递语音提示
                            } else {
                                SoundPlayUtils.play(13);//其它种类垃圾开箱语音投递
                            }
//                                                    //打开投口
//                                                    UserTeleFragment.getInstance().mActivity.serialPortUtils.sendSerialPort("androidC51:" + Constant.numberMap.get(Constant.categoryMap.get(UserInfoCache.category)[3]) + ";");
                            //跳转到开箱投递页面
                            UserTeleFragment.getInstance().mActivity.getFragmentManager().beginTransaction()
                                    .replace(R.id.mid, UserOpenDeliverFragment.getInstance())
                                    .commit();
                            //----------测试用-------------//


                            //手机号验证
//                            OkhttpHelper.postRequest(Constant.baseUrl + "machine/verification/userLoginByPhoneNum",
//                                    new Gson().toJson(new PhoneNumLoginReq(LocalStorage.getDeviceID(MyApplication.getInstance()), UserInfoCache.telNum)),
//                                    new Callback() {
//                                        @Override
//                                        public void onFailure(Call call, IOException e) {
//                                            Log.i("info", "userLoginByPhoneNum_onFailure: " + e.getMessage());
//                                            //继续倒计时
//                                            UserTeleFragment.getInstance().mActivity.mBackAndTimer.setTime(UserTeleFragment.getInstance().mActivity.mBackAndTimer.getCurrentTime()).start();
//                                        }
//
//                                        @Override
//                                        public void onResponse(Call call, Response response) {
//                                            try {
//                                                RespCommonResult result = new Gson().fromJson(response.body().string(), RespCommonResult.class);
//                                                if (result.getStateCode() == 1) {
//                                                    UserInfoCache.loginState = true;
//                                                    if (UserInfoCache.category.equals(Constant.bottle)) {
//                                                        SoundPlayUtils.play(17);//饮料瓶投递语音提示
//                                                    } else {
//                                                        SoundPlayUtils.play(13);//其它种类垃圾开箱语音投递
//                                                    }
////                                                    //打开投口
////                                                    UserTeleFragment.getInstance().mActivity.serialPortUtils.sendSerialPort("androidC51:" + Constant.numberMap.get(Constant.categoryMap.get(UserInfoCache.category)[3]) + ";");
//                                                    //跳转到开箱投递页面
//                                                    UserTeleFragment.getInstance().mActivity.getFragmentManager().beginTransaction()
//                                                            .replace(R.id.mid, UserOpenDeliverFragment.getInstance())
//                                                            .commit();
//
//                                                }
//                                            } catch (IOException e) {
//                                                //继续倒计时
//                                                UserTeleFragment.getInstance().mActivity.mBackAndTimer.setTime(UserTeleFragment.getInstance().mActivity.mBackAndTimer.getCurrentTime()).start();
//                                            }
//
//                                        }
//                                    });
                        } else {
                            //关闭对话框
                            if (mOnCloseDialogListener != null) {
                                mOnCloseDialogListener.onCloseDialog();
                            }
                        }
                    }
                });
            }
        });
        mThread.start();
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //确定
            case R.id.btn_ok:
                //跳转到打开箱门页面
                mIsConfirm = true;
                mOnCloseDialogListener = null;
                closeDialog();
                break;
            //关闭
            case R.id.btn_close:
                mIsConfirm = false;
                closeDialog();
                break;
            default:
                break;
        }
    }

    /**
     * 关闭对话框
     */
    public void closeDialog() {
        if (mThread != null) {
            mThread.interrupt();
        }
    }
}
